<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/8
 * Time: 10:54
 */

namespace Admin\Controller;

use OSS\OssClient;
use OSS\Core\OssException;

class ImgController extends AdminController
{

    public function _myinitialize()
    {
        $this->db=M('img');
        $this->username = session('user_auth.username');
    }

    //图片列表
    public function lst()
    {
        $where['status']=1;
        $lists=$this->db->where($where)->order("id desc")->select();
        $this->assign('lists',$lists);
        $this->display();
    }

    //图片上传
    public function upload()
    {

        $upload = new \Think\Upload();
        $upload->maxSize = 20 * 1024 * 1024;
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg', 'bmp');// 设置附件上传类型
        $upload->rootPath = './'; // 设置附件上传根目录
        $upload->savePath = "./Uploads/ad/";
        $upload->autoSub = false;//不用子目录保存文件，所有的表都保存在同一个目录下
        $upload->saveName = date("Ymdhis") . mt_rand(1000, 9999);
        $upload->replace = true;//存在同名文件覆盖

        $info = $upload->upload();
        if (!$info) {// 上传错误提示错误信息
            $re=array(
                "status"=>1,
                "error"=>$upload->getError()
            );
        } else {// 上传成功
            vendor('OSS.autoload');//引入OSS根目录下自动加载类
            vendor('OSS.OSSConfig');//引入配置文件
            foreach ($info as $k => $v) {
                $local_file_path = $v['savepath'] . $v['savename'];
                $accessKeyId = \OSSConfig::OSS_ACCESS_ID;
                $accessKeySecret = \OSSConfig::OSS_ACCESS_KEY;
                $endpoint = \OSSConfig::OSS_ENDPOINT;
                $bucket = \OSSConfig::OSS_BUCKET;
                //图片上传到OSS上后在bucket下的路径+文件名,如：ad/2017/0808/201708090951389678.jpg
                $object = "ad/" . date("Y") . "/" . date("md") . "/" . $v['savename'];
                //图片在本地的路径+文件名
                $filePath = $local_file_path;

                try {
                    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, true);
                    $oss_re = $ossClient->uploadFile($bucket, $object, $filePath);
                    $oss_file_path = $oss_re['oss-request-url'];//OSS上的地址
                } catch (OssException $e) {
                    $error=array(
                        "status"=>0,
                        "error"=>$e->getMessage()
                    );
                }
                @unlink($local_file_path);
                //将返回的图片信息存入数据库
                $data = array(
                    "oss_img_url" => $oss_file_path,
                    'status' => 1,
                    'update_time' => time(),
                    'update_user' => $this->username,
                );
                $id=$this->db->add($data);
                if($id===false){
                    $re=array(
                        "status"=>0,
                        "error"=>$this->db->getError()
                    );
                }else{
                    $re=array(
                        "status"=>1,
                        "error"=>"上传成功"
                    );
//                    $this->redirect('lst');
                }
            }
        }
        $this->ajaxReturn($re);
    }

    //图片删除
    public function del()
    {
        $id = I('id');
        if (!empty($id)) {
            $data = array(
                'status' => 0,
                'update_time' => time(),
                'update_user' => $this->username,
            );
            $this->db->where(array('id' => $id))->save($data);
            $this->redirect('lst');
        }
    }
}